package com.yst.gateway.monitor.controllers;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.groups.Default;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.yst.gateway.monitor.common.beans.Response;
import com.yst.gateway.monitor.common.utils.UUIDGenerator;
import com.yst.gateway.monitor.common.validator.groups.Groups;
import com.yst.gateway.monitor.models.QzChannelInterface;
import com.yst.gateway.monitor.models.QzChannelInterfaceAttachment;
import com.yst.gateway.monitor.services.QzInterfaceService;

/**
 * 前置系统接口管理访问控制类
 * @author sheungxin
 *
 */
@RestController
@RequestMapping(value = "/qz/inters")
public class QzInterfaceController {
	
	@Autowired
	private QzInterfaceService qzChannelService;
	
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public Response post(@RequestBody @Validated({Default.class,Groups.Add.class}) QzChannelInterface qzInter){
		Response response=new Response();
		qzInter.setChannelInterId(UUIDGenerator.getUUID());
		if(qzChannelService.add(qzInter)){
			response.success(qzInter.getChannelInterId());
		}else{
			response.failure();
		}
		return response;
	}
	
	@RequestMapping(value = "/modify",method = RequestMethod.PUT)
	public Response put(@RequestBody @Validated({Default.class,Groups.Modify.class}) QzChannelInterface qzInter){
		Response response=new Response();
		if(qzChannelService.update(qzInter)){
			response.success();
		}else{
			response.failure();
		}
		return response;
	}
	
	@RequestMapping(value = "/lock/{id}",method = RequestMethod.PUT)
	public Response lock(@PathVariable("id") String id){
		Response response=new Response();
		if(qzChannelService.updateStatusById(id)){
			response.success();
		}else{
			response.failure();
		}
		return response;
	}
	
	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	public Response get(@PathVariable("id") String id) {
		Response response=new Response();
		QzChannelInterface qzInter=qzChannelService.getById(id);
		if(qzInter!=null){
			response.success(qzInter);
		}else{
			response.failure();
		}
		return response;
	}
	
	
	@RequestMapping(value = "/getByPage", method = RequestMethod.POST)
	public Response getByPage(@RequestBody Map<String, Object> paramMap) {
		Response response=new Response();
		int pageNum=1;
		int pageSize=10;
		try{
			if(paramMap.containsKey("pageNum")){
				pageNum=(Integer)paramMap.get("pageNum");
				paramMap.remove("pageNum");
			}
			if(paramMap.containsKey("pageSize")){
				pageSize=(Integer)paramMap.get("pageSize");
				paramMap.remove("pageSize");
			}
		}catch (Exception e) {e.printStackTrace();
		}
		response.success(qzChannelService.getByPage(pageNum, pageSize, paramMap));
		return response;
	}
	
	@RequestMapping(value = "/del/{id}",method = RequestMethod.PUT)
	public Response del(@PathVariable("id") String id){
		Response response=new Response();
		if(qzChannelService.delById(id)){
			response.success();
		}else{
			response.failure();
		}
		return response;
	}
	
	@RequestMapping(value = "/getSpMap/{interId}",method = RequestMethod.GET)
	public Response getSpBank(@PathVariable("interId") String interId){
		Response response=new Response();
		Map<String, Object> spMap=qzChannelService.getSpMap(interId);
		if(spMap!=null){
			response.success(spMap);
		}else{
			response.failure();
		}
		return response;
	}
	
	@RequestMapping(value = "/addSpBank",method = RequestMethod.POST)
	public Response addSpBank(@RequestBody Map<String, Object> paramMap){
		Response response=new Response();
		if(qzChannelService.addSpBank(paramMap)){
			response.success();
		}else{
			response.failure();
		}
		return response;
	}
	
	@RequestMapping(value = "/modifySpBank",method = RequestMethod.PUT)
	public Response modifySpBank(@RequestBody Map<String, Object> paramMap){
		Response response=new Response();
		if(qzChannelService.modifySpBank(paramMap)){
			response.success();
		}else{
			response.failure();
		}
		return response;
	}
	
	@RequestMapping(value = "/addDeploy",method = RequestMethod.POST)
	public Response addDeploy(@RequestBody Map<String, Object> paramMap){
		Response response=new Response();
		if(qzChannelService.addDeploy(paramMap)){
			response.success();
		}else{
			response.failure();
		}
		return response;
	}
	
	@RequestMapping(value = "/modifyDeploy",method = RequestMethod.PUT)
	public Response modifyDeploy(@RequestBody Map<String, Object> paramMap){
		Response response=new Response();
		if(qzChannelService.modifyDeploy(paramMap)){
			response.success();
		}else{
			response.failure();
		}
		return response;
	}
	
	@RequestMapping(value = "/checkName", method = RequestMethod.GET)
	public boolean checkName(@RequestParam String interName,@RequestParam(required=false) String interId) {
		return qzChannelService.checkInterName(interName,interId);
	}
	
	@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
	public void fileUpload(HttpServletRequest request, HttpServletResponse response) {
		QzChannelInterfaceAttachment attachment = new QzChannelInterfaceAttachment();
		attachment.setChannelInterId(request.getParameter("interid"));
		System.out.println("接口编号：" + request.getParameter("interid"));
		qzChannelService.fileUpload(attachment, request, response);
	}
	
	@RequestMapping(value = "/getAttchs/{id}", method = RequestMethod.GET)
	public Response getAttachements(@PathVariable("id") String id) {
		Response response = new Response();
		response.success(qzChannelService.getAttchementList(id));
		return response;
	}
	
	@RequestMapping(value = "/fileDel/{id}", method = RequestMethod.PUT)
	public Response fileDel(@PathVariable("id") String id) {
		Response response = new Response();
		boolean flag = qzChannelService.filedelById(id);
		if(flag) {
			response.success();
		}else {
			response.failure();
		}
		return response;
	}
	
}
